package com.nsl.my_youlai.controller;

import com.nsl.my_youlai.common.result.Result;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/v1")
public class Hello {

    @GetMapping("/hello")
    public String helloController(){
        return "hello";
    }

    @GetMapping("/passArrayByGet")
    public Result<String> passArrayByGet(@RequestParam(value = "arr[]")String[] arr){
        return Result.success(Arrays.toString(arr));
    }

    //@RequestParam(value = "arr")String[] arr,
    @PostMapping("/passArrayByPostForm")
    public Result<String> passArrayByPostForm(HttpServletRequest request, String name, @RequestParam(value = "arr[]") String arr[]){
        Enumeration<String> enumerationAttributeNames=request.getAttributeNames();
        while(enumerationAttributeNames.hasMoreElements()){
            String attributeName=enumerationAttributeNames.nextElement();
            log.info("enumerationAttributeNames = {} , value= {} ",attributeName,request.getAttribute(attributeName));
        }
//        Enumeration<String> enumerationParameterNames=request.getParameterNames();
//        while(enumerationParameterNames.hasMoreElements()){
//            String parameterName=enumerationAttributeNames.nextElement();
//            log.info("enumerationParameterNames = {}  , value= {}",enumerationParameterNames.nextElement(),request.getParameter(parameterName));
//        }
        System.out.println(request.getParameter("arr[]"));
        System.out.println(request.getAttribute("arr"));
        System.out.println(request.getParameter("name"));
        System.out.println(name);
        System.out.println(Arrays.toString(arr));
        return Result.success(Arrays.toString(arr));
    }

    @PostMapping("/passArrayByPostJson")
    public Result<String> passArrayByPostJson(@RequestBody Map map){
        String name= (String) map.get("name");
        List<String> list= (List<String>) map.get("arr");
        System.out.println(name);
        System.out.println(list);
        return Result.success(list.toString());
    }

    @PostMapping("/passArrayByPostFormString")
    public Result<String> passArrayByPostFormString(String name,  String arrString){
        System.out.println(name);
        System.out.println(arrString.toString());
        return Result.success(arrString.toString());
    }
}
